package com.example.bigdata.kafka.producer;

import org.apache.kafka.clients.producer.Partitioner;
import org.apache.kafka.common.Cluster;

import java.util.Map;

/**
 *  定义 Partition 分区算法
 */
public class PartitionSample implements Partitioner {

    @Override
    public int partition(String s, Object key, byte[] bytes, Object value, byte[] bytes1, Cluster cluster) {
        /**
         *  key-1
         *  key-2
         *  key-3
         */
        String keyStr = key +"";
        String keyInt = keyStr.substring(4);
//        System.out.println("KeyStr : "+keyStr +"\tkeyInt : " + keyInt);
        int i = Integer.parseInt(keyInt);
        return i%2 ;
    }

    @Override
    public void close() {

    }

    @Override
    public void configure(Map<String, ?> map) {

    }
}
